package org.car.world.controller.shop;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.car.world.common.util.DateFormatUtil;
import org.car.world.core.entily.MerchantsTable;
import org.car.world.service.CommentsService;
import org.car.world.service.MerchantsService;
import org.car.world.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 * @ClassName: HomeController
 * @Description:我的服务
 * @date Nov 22, 2018
 */
@Controller
@RequestMapping("shop")
public class MerchantsServerController {

    @Autowired
    MerchantsService merchantsService;

    @Autowired
    CommentsService commentsService;

    @Autowired
    OrderService orderService;


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: doindex_init
     * @Description: 访问我的服务
     */
    @RequestMapping("domyservice")
    public String domyservice_init(@RequestParam(value = "phone", defaultValue = "", required = false) String phone, Model mo, HttpSession session) {
        List<MerchantsTable> datamerlist = new ArrayList<MerchantsTable>();
        if (StringUtils.isEmpty(phone)) {
            datamerlist = (List<MerchantsTable>) session.getAttribute("Merchants");
            //datamerlist = merchantsService.selectphone("17630389495");
        } else {
            datamerlist = merchantsService.selectphone(phone);
        }
        String[] busdata;
        Integer merid = 0;
        String InitBusin = "";
        String myphone = "";
        List<String> datalist = new ArrayList<String>();
        for (MerchantsTable mer : datamerlist) {
            InitBusin = mer.getMerchantsBusinesspro();
            myphone = mer.getMerchantsPhone();
            busdata = mer.getMerchantsBusinesspro().split("、");
            merid = mer.getMerchantsId();
            for (int i = 0; i < busdata.length; i++) {
                if (!"".equals(busdata[i]) && busdata[i] != "") {
                    datalist.add(busdata[i]);
                }
            }
        }
        if (datalist.size() > 0) {
            mo.addAttribute("Id", merid);
            mo.addAttribute("Phone", myphone);
            mo.addAttribute("InitBusin", InitBusin);
            mo.addAttribute("MySer", datalist);
        }

        return "shop/myservice";
    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: doindex_init
     * @Description: 更新我的服务
     */
    @RequestMapping("UpBusinesspro")
    @ResponseBody
    public boolean UpBusinesspro(MerchantsTable mer) {
        boolean flag = merchantsService.updateByPrimaryBusi(mer);
        if (flag) {
            return flag;
        }
        return flag;
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws ParseException
     * @throws
     * @Title: doindex_init
     * @Description: 访问我的店铺
     */
    @RequestMapping("domyshop")
    public String domyshop(Model mo, HttpSession session) throws ParseException {
        List<MerchantsTable> datamerlist = new ArrayList<MerchantsTable>();
        List<Object> imglist = new ArrayList<Object>();
        List<Object> businlist = new ArrayList<Object>();
        String[] shopimg;
        String[] businesspro;
        datamerlist = (List<MerchantsTable>) session.getAttribute("Merchants");
        //datamerlist = merchantsService.selectphone("17630389495");
        if (datamerlist.size() > 0) {

            for (MerchantsTable mer : datamerlist) {
                businesspro = mer.getMerchantsBusinesspro().split("、");
                shopimg = mer.getMerchantsPicture().split(",");

                for (int i = 0; i < businesspro.length; i++) {
                    if (!"".equals(businesspro[i]) && businesspro[i] != "") {
                        businlist.add(businesspro[i]);
                    }
                }

                for (int i = 0; i < shopimg.length; i++) {
                    if (!"".equals(shopimg[i]) && shopimg[i] != "") {
                        imglist.add(shopimg[i]);
                    }
                }
                //计算商家注册天数
                java.util.Date dt = new Date();
                long nowString = dt.getTime();
                long zcString = mer.getMerchantsOpeningdate().getTime();
                String datec = DateFormatUtil.TimeDifference(nowString, zcString, 1);
                int count = orderService.selectByPrimaryListOkCount(mer.getMerchantsId());

                mo.addAttribute("MerList", datamerlist);
                mo.addAttribute("ImgList", imglist);
                mo.addAttribute("BusinList", businlist);
                mo.addAttribute("Count", count);
                mo.addAttribute("Datec", datec.split("-")[1]);
                mo.addAttribute("PlCount", commentsService.selectAllPlNum(mer.getMerchantsId()));
            }
        }
        return "shop/myshopht";
    }


    /**
     * @param @return 参数
     * @return boolean    返回类型
     * @throws
     * @Title: doindex_init
     * @Description: 验证密码
     */
    @RequestMapping("shopwd")
    @ResponseBody
    public boolean ShopWd(Integer id, String odlpd) {
        MerchantsTable mer = new MerchantsTable();
        mer.setMerchantsId(id);
        mer.setMerchantsPwd(odlpd);
        int flag = merchantsService.SelectByPrimaryKeyPwd(mer);
        if (flag > 0) {
            return true;
        }
        return false;
    }


    /**
     * @param @return 参数
     * @return boolean    返回类型
     * @throws
     * @Title: doindex_init
     * @Description: 修改密码
     */
    @RequestMapping("upshopwd")
    @ResponseBody
    public boolean upshopwd(MerchantsTable mer) {
        return merchantsService.updateByPrimaryPwd(mer) > 0;
    }
}
